/**
 * 
 */
package com.ar.myshares.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

import com.ar.myshares.daos.UserDAO;
import com.ar.myshares.model.User;

/**
 * @author mac
 *
 */
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDAO userDAO;

	@Autowired
	private UserAssembler userAssembler;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ar.myshares.services.UserService#getUser(long)
	 */
	@Override
	@Transactional(readOnly = true)
	public User getUser(long id) {
		return userDAO.getUser(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ar.myshares.services.UserService#getAllUsers()
	 */
	@Override
	@Transactional(readOnly = true)
	public List<User> getAllUsers() {
		return userDAO.getAllUsers();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ar.myshares.services.UserService#saveUser(com.ar.myshares.model.User)
	 */
	@Override
	@Transactional
	public User saveUser(User user) {
		return userDAO.saveUser(user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ar.myshares.services.UserService#removeUser(com.ar.myshares.model
	 * .User)
	 */
	@Override
	@Transactional
	public void removeUser(User user) {
		userDAO.removeUser(user);
	}

	@Override
	@Transactional(readOnly = true)
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User userByUsername = userDAO.getUserByUsername(username);
		if (userByUsername != null) {
			return userAssembler.buildFromUserEntity(userByUsername);
		} else {
			throw new UsernameNotFoundException("User name doesn't exist");
		}
	}

}
